<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>新闻查看</title>
<style type="text/css">
/*新闻展示样式*/
.news_show{ margin:0 auto;}
.news_show .show_title {text-align:center; font-size:14px; border-bottom:1px dotted #c7c7c7; padding-bottom:15px;margin-bottom:20px;}
.news_show h2{ font-size:24px;}
.news_show span{ padding-right:10px;}
.news_show .paging{ text-align:center;}
.news_show a{ color:#0871A5;}
.news_show .content{font-size:15px;text-indent:28px;}
.news_show .operator{padding-top:25px; text-align:center; }
/***********/
.table,.table th,.table td{border:1px solid #cfe1f9;}
.table{border-bottom:#cfe1f9 solid 2px;border-collapse:collapse;line-height:38px;width:90%;margin:15px auto;}
.table th{background:#eef7fc;color: #185697;padding:0 6px;}
.table td{padding:0 12px;}
.form-btn{height: 26px;border: 1px #949494 solid;padding: 0 10px;cursor: pointer;background:#fff;margin-right:10px;}
.comments{  width:100%; overflow:auto; word-break:break-all;  }
</style>
<script src="common/jquery-3.1.1.min.js" type="text/javascript"></script>
</head>
<?php   
   require_once 'common/news.dao.php';
   require_once 'common/newsclass.dao.php';
   require_once 'common/user.dao.php';
   require_once 'common/reply.dao.php';
   require_once 'common/checkFormlib.php';
   $newsid="";
   if(!empty($_POST)){
	  //启动Session
	  session_start(); 
	  $newsid=$_POST['newsid'];      
	  if(isset($_SESSION['userinfo'])){
	      $userinfo = $_SESSION['userinfo'];  //获取用户信息
		  $userid=$userinfo['id'];  //用户名
		  $comment=isset($_POST['commnet'])?test_input($_POST['commnet']):'';		
		  $result=addReply($userid,$newsid,$comment);
		  if($result){
		       echo "<script>alert('成功发表评论>>>>')</script>";
		  }
		  
      }else{
	     //用户信息不存在，说明用户没有登录
	     echo "<script>alert('登录后才可发表评论，请先登录>>>>')</script>";		
	  }  
   }
   if(!empty($_GET)){
      $newsid=$_GET['newsid'];
   }   
   if($newsid!=""){
       	$rst=findNewsById($newsid);
		$author_rst=findUserById($rst['uid']);
        $author=$author_rst['uname'];
		$newsclass_rst=findNewsClassById($rst['classid']);		
		$newsclass=$newsclass_rst['classname']; 
		//更新新闻访问量
		$viewcount=$rst['viewcount']+1;
		$result=updateViewCount($newsid);
		if(!$result){echo "<script>alert('fail update viewcount.');</script>";}
		//新闻点赞相关
		$likecount=$rst['likecount'];
         //新闻评论相关		 
		 $commentRst=findReplyByNewsid($newsid);
		 $commentCount=count($commentRst);
		 
   }else{
	  header("location:newslist.php");     
   }
  
?>
<body>
    <div class="news_show">
		<div class="show_title"> 
			<h2><?php echo $rst['title'];?></h2>
			<span>时间：<?php echo $rst['publishtime'];?></span>
			<span>分类：<?php echo $newsclass;?></span>
			<span>作者：<?php echo $author;?></span>
            <span>访问量：<?php echo $viewcount;?></span>            
		</div>
		<div class="content"><?php echo htmlspecialchars_decode(trim($rst['content']));?> </div>
       <div class="operator">
              <button id="vote"  rel="<?php echo $newsid;?>">点赞</button> 
              <span id="likes"><?php echo $likecount;?></span>             
        </div>
        <div class="show_comment">
         <table class="table">
            <form method="post"  action="newsdetail.php">
            <tr colspan="2">
              <th>评论内容:</th>
              <td><textarea name="commnet" class="comments"></textarea> 
                  <input type="hidden" name="newsid" value="<?php echo $newsid?>">
              </td>             
              </tr>
              <tr>
			 <td colspan="2" align="center">
                 <input type="submit" value="确认发布" class="form-btn">　
                 <input type="reset" value="重新填写" class="form-btn">
             </td>
		   </tr>
          </form>
        <tr>
          <td colspan="2">共计<?php echo $commentCount;?>条评论</td>
        </tr> 
         <?php if(!empty($commentRst)){
		     foreach($commentRst as $row){
				$comment_user=$row['uid'];
				$comment_content=$row['content'];
				$comment_publishtime=$row['publishtime'];
				$user_rst=findUserById($comment_user);
				$user_uname=$user_rst['uname'];
				$user_headimg=$user_rst['headimg'];
				$headimg_file="./headimg/".$user_headimg;				
				?>
         <tr>
            <td colspan="2">用户<img src="<?php echo $headimg_file;?>" width="30" height="30">
               <span><?php echo $user_uname;?></span>
               <span><?php echo $comment_publishtime;?></span>
                说:
			   <span><?php echo $comment_content;?></span></td>
         </tr>
                <?php
			 }
		 }
		 ?>      
        </table>
         
        </div>	
	</div>
   <script type="text/javascript">
     $(document).ready(function() {
         $("#vote").click(function(){		   
	      $.ajax(
	       { url:"updatelike.php",//处理数据的地址
             type:"post",//数据提交形式
             data:{"id":<?php echo $newsid;?>,"likecount":<?php echo $likecount;?> },//需要提交的数据
            success:function(data){//数据返回成功的执行放大
              if(data==false){
				 alert("赞过了");     
			  }else{
			    alert('点赞数+1');				
				$("#likes").html(data);
			  }               	 
	       }
	    });	 
     });
   });
   </script>
</body>
</html>